<?php


namespace app\common\model;
use think\Model;
use think\Db;

class Goods extends Model
{



    /*
     * 获取商品分类-废弃
     * */
    public function goodInfo($data_array)
    {
        $page = $data_array['page'] ? $data_array['page'] : 1;
        $url  = config('fastadmin.api_url');
        $type = [['id'=>0,'title'=>'推荐商品']];
        $cate = Db::name('cate_goods')->where(['pid'=>0,'status'=>1])->field('id,title')->select();
        // 合并数组
        $good = array_merge($type,$cate);
        $where= ['is_del'=>1,'status'=>1,'state'=>2];

        foreach($good as $key => $val)
        {
            if(empty($val['id']))
            {
                $goods = Db::name('goods')->where($where)->field('id as goods_id,name,image,money,integer,num')->page($page,10)->select();
                foreach($goods as &$val1)
                {
                    $val1['image'] = $url.$val1['image'];
                }
                $good[$key]['goods'] = $goods;
            }else{
                $good[$key]['goods'] = Db::name('cate_goods')->where('pid',$val['id'])->field('id,title')->page($page,10)->select();
                foreach($good[$key]['goods'] as $key => &$val)
                {

                    $val['ca_goods'] = Db::name('goods')->where(['cate_id'=>$val['id']])->field('id as goods_id,name,image,money,integer,num')->page($page,10)->select();
                    foreach($val['ca_goods'] as &$val2)
                    {
                        $val2['image'] = $url.$val2['image'];
                    }
                }
            }
        }
        return $good;
    }


    

    /*
     * 详情 
     * */
    public function details($data_array)
    {
        $url  = config('fastadmin.api_url');
        $data = Db::name('goods')->where('id',$data_array['goods_id'])->field('id as goods_id,name,image,integer,money,num,item,desc,activity,start_time,end_time')->find();
        $data['start_time'] = time();
        $data['item'] = json_decode($data['item'],true);
        foreach($data['item'] as $key => $val)
        {
            
            if(!isset($val['vip_money']))$data['item'][$key]['vip_money'] = 0;
        }
        $address = "";
        $img   = [];
        $image = explode(',',$data['image']);
        foreach($image as $key => &$val)
        {
            $img[] = $url.$val;
        }
        $data['image'] = $img;
        $res = ['data'=>$data,'address'=>$address];

        if($data['activity'] == 1)
        {
            $countdown = $data['end_time'] - time();
            if($countdown < 0)return ['code'=>1,'data'=>$res,'msg'=>'该活动已结束'];
            $data['countdown'] = round($countdown / 86400,0);
        }
        return ['code'=>'1','msg'=>'查询成功','data'=>$res];
    }



    // 分类
    public function cate($data_array)
    {
        $url   = config('fastadmin.api_url');
        $page  = $data_array['page'] ? $data_array['page'] : 1;
        $where = $wheres = $state = [];

        $state= [['id'=>0,'title'=>'推荐商品']];
        if($data_array['type'])
        {
            $wheres['cate_pid'] = ['=',$data_array['id']];
            $wheres['cate_id'] = ['=',$data_array['type']];
        }
        
        $field= ['id,title'];
        $cate = Db::name('cate_goods')
            ->where(['status'=>1,'pid'=>0])
            ->field($field)
            ->select();
        
        $merge = array_merge($state,$cate);

        // 存在 check 1 选中
        if($data_array['id'])
        {
            foreach($merge as &$val)
            {
                if(!empty($val['id']))
                {
                    $val['goods'] = Db::name('cate_goods')->where(['pid'=>$val['id'],'status'=>1])->field($field)->select();
                    if($val['id'] == $data_array['id'])
                    {
                        $val['check'] = 1;
                    }else{
                        $val['check'] = 0;
                    }
                }else{
                    $val['check'] = 0;
                }
            }
        }else{
            foreach($merge as &$val)
            {
                if(!empty($val['id']))
                {
                    $val['goods'] = Db::name('cate_goods')->where('pid',$val['id'])->field($field)->select();
                    $val['check'] = 0;
                }else{
                    $val['check'] = 1;
                }
            }
        }

        if(!$data_array['type'])
        {

            if($data_array['id'])
            {
                $goods = Db::name('goods')
                    ->where('cate_pid',$data_array['id'])
                    // ->where(['status'=>1,'is_del'=>1,'state'=>1,'type'=>1])
                    ->where(['status'=>1,'is_del'=>1,'type'=>1])
                    ->field('id as goods_id,name,image,money,integer,num,item')
                    ->page($page,10)
                    ->select();
            }else{
                $goods = Db::name('goods')
                    ->where(['status'=>1,'is_del'=>1,'state'=>2,'type'=>1])
                    ->field('id as goods_id,name,image,money,integer,num,item')
                    ->page($page,10)
                    ->select();
            }
        }else{
            $goods = Db::name('goods')
                ->where($wheres)
                ->where(['status'=>1,'is_del'=>1,'type'=>1])
                ->field('id as goods_id,name,image,money,integer,num,item')
                ->page($page,10)
                ->select();
        }
        if($goods)
        {
            foreach($goods as $key=>&$val)
            {
                $image = explode(',',$val['image']);
                $val['image'] = $url.$image[0];
                $item = json_decode($val['item'],true);
                $item = current($item);
                $goods[$key]['money']   = $item['intro'];
                $goods[$key]['integer'] = $item['integral'];
                $goods[$key]['num']     = $item['stock'];
                if(!isset($item['vip_money'])){
                    $goods[$key]['vip_money'] = 0;
                }else{
                    $goods[$key]['vip_money'] = $item['vip_money'];
                }
            }
        }
        $res = ['merge'=>$merge,'goods'=>$goods];

        return $res;
    }
    
}